package library;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DatabaseHandler extends SQLiteOpenHelper {
 
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "db528948130";
 
    // Login table name
    private static final String TABLE_LOGIN = "login";
 
    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_IDUSER = "idUser";
    private static final String KEY_FIRSTNAME = "firstname";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_IDSTATUS = "idStatus";
    private static final String KEY_IDCONVERSATION = "idConversation";
    private static final String KEY_SEX = "sex";
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_IDUSER + " INTEGER,"
                + KEY_FIRSTNAME + " TEXT,"
                 + KEY_EMAIL + " TEXT,"
                + KEY_IDSTATUS + " INTEGER,"
                + KEY_IDCONVERSATION + " INTEGER,"
        		+ KEY_SEX + " TEXT)";
        db.execSQL(CREATE_LOGIN_TABLE);
    }
 

    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
        onCreate(db);
    }
 
    /**
     * Storing user details in database
     * */
    public void addUser(Integer idUser,String firstname, String email,Integer idStatus,Integer idConversation, String sex) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_IDUSER, idUser);
        values.put(KEY_FIRSTNAME, firstname);
        values.put(KEY_EMAIL, email);
        values.put(KEY_IDSTATUS, idStatus);
        values.put(KEY_IDCONVERSATION, idConversation);
        values.put(KEY_SEX, sex);
        db.insert(TABLE_LOGIN, null, values);
        db.close();
    }
    
    
    
    /**
     * Update statu user
     * */
    public void updateStatus(int idStatus,String email){
    	  SQLiteDatabase db = this.getWritableDatabase();
    	  ContentValues values = new ContentValues();
    	  values.put(KEY_IDSTATUS, idStatus);
    	  db.update(TABLE_LOGIN,values,"email='"+email+"'",null);
    	  db.close();
    }
    
    
    
    /**
     * Update statu user
     * */
    public void updateConversation(int idConversation,String email){
    	  SQLiteDatabase db = this.getWritableDatabase();
    	  ContentValues values = new ContentValues();
    	  values.put(KEY_IDCONVERSATION, idConversation);
    	  db.update(TABLE_LOGIN,values,"email='"+email+"'",null);
    	  db.close();
    }
     
     
    
    /**
     * Getting user data from database
     * */
    public HashMap<String, String> getUserDetails(){
        HashMap<String,String> user = new HashMap<String,String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        cursor.moveToFirst();
        if(cursor.getCount() > 0){
        	user.put("idUser", cursor.getString(1));
            user.put("firstname", cursor.getString(2));
            user.put("email", cursor.getString(3));
            user.put("idStatus", cursor.getString(4));
            user.put("idConversation", cursor.getString(5));
            user.put("sex", cursor.getString(6));
        }
        cursor.close();
        db.close();
        return user;
    }
 
    /**
     * Getting user login status
     * return true if rows are there in table
     * */
    public int getRowCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();
        return rowCount;
    }
     
    /**
     * Re crate database
     * Delete all tables and create them again
     * */
    public void resetTables(){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_LOGIN, null, null);
        db.close();
    }
 
}